package com.xiangyang.pos.bean;

import android.content.Context;

import com.google.gson.Gson;
import com.xiangyang.pos.http.RequestParameters;
import com.xiangyang.pos.utils.CryptoUtil;
import com.xiangyang.pos.utils.SharedUtils;

import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/21 0021.
 *
 * 订单支付数据源
 *
 */

public class PayOrderBean {

    private String open_id;
    private String open_key;
    private String timestamp;
    private String data;
    private String sign;


    public PayOrderBean(Context context) {

        open_id = "21ce25d680d895418b204564afd5f92b";
        open_key = "ff2d78c73530f141bdf155786068ea0a";
        timestamp = getTime();

        getData(context);

    }

    /*
    * 主扫
    * 设置交易数据(订单流水号，支付类型，原始交易金额，实际交易金额，客户条码）
    * */
    private void setData(String out_on, String pmt_tag, String original_amount, String trade_amount, String auth_code) {
        Map<String, String> map = new HashMap();
//        RequestParameters map = new RequestParameters();

        map.put("out_no", out_on);//订单流水号(KX_+支付方式_+时间戳）
        map.put("pmt_tag", pmt_tag);//交易类型，Weixin 微信，Alipay2 支付宝
        map.put("original_amount", original_amount);//原始交易金额
        map.put("trade_amount", trade_amount);//实际交易金额
        map.put("auth_code", auth_code);//客户条码

        Gson gson = new Gson();
        String str = gson.toJson(map);

        try {
            data = CryptoUtil.AESEncrypt(str, open_key);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        data = str.substring(14, str.length() - 1);


        setSign();
    }

    //    设置sign签名
    private void setSign() {

        String str = "data=" + data + "&open_id=" + open_id + "&open_key=" + open_key + "&timestamp=" + timestamp;

        str = CryptoUtil.SHA1(str);

        try {
            str = CryptoUtil.Md5(str);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        sign = str;
    }

    //    支付需要上传的交易参数
    public RequestParameters getPayData() {
        RequestParameters parameters = new RequestParameters();
        parameters.put("open_id", open_id);
        parameters.put("timestamp", timestamp);
        parameters.put("sign", sign);
        parameters.put("data", data);
        return parameters;
    }

    //    获取时间戳
    private String getTime() {
        long time = System.currentTimeMillis() / 1000;//获取系统时间的10位的时间戳
        String str = String.valueOf(time);
        return str;
    }

    private void getData(Context context) {
        String pmt_tag = (String)SharedUtils.getStringPrefs(context,"pmt_tag","");
        int original_amount = SharedUtils.getIntPrefs(context,"all_money",-1);
        String auth_code =  SharedUtils.getStringPrefs(context,"auth_code","");


        String out_on = "KX-" + pmt_tag +"-"+ timestamp;
        SharedUtils.setStringPrefs(context, "out_on",out_on);

        setData(out_on, pmt_tag, original_amount + "", original_amount + "", auth_code);
    }

}
